/**
 * @program: LeetCode
 * @description: LeetCode : 738. 单调递增的数字
 * @author: WXY
 * @create: 2023-01-24 16:03
 * @Version 1.0
 **/
public class Num783_monotoneIncreasingDigits {
    public static int monotoneIncreasingDigits(int n) {
        String s = String.valueOf(n);
        char[] str = s.toCharArray();
        int start = str.length ;
        for (int i = str.length - 1; i > 0; i--) {
            if (str[i] < str[i - 1]) {
                str[i-1]--;
                start = i;
            }
        }
        for (int i = start; i < str.length; i++) {
            str[i] = '9';
        }

        int ans = Integer.valueOf(new String(str));
        return ans;
    }

    public static void main(String[] args) {
        int n = 2152;
        System.out.println(monotoneIncreasingDigits(n));

    }
}
